package com.guducat.collegeWeb.controller;

import com.guducat.collegeWeb.dto.response.*;
import com.guducat.collegeWeb.service.HomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/home")
@Tag(name = "首页内容管理", description = "提供首页轮播图、校园看点、英雄区域等内容的获取功能")
public class HomeController {
    private final HomeService homeService;

    public HomeController(HomeService homeService) {
        this.homeService = homeService;
    }

    @Operation(summary = "获取首页轮播图数据")
    @GetMapping("/slides")
    public ResponseEntity<List<HomeSlideDTO>> getHomeSlides() {
        List<HomeSlideDTO> slides = homeService.getActiveHomeSlides();
        return ResponseEntity.ok(slides);
    }

    @Operation(summary = "获取校园看点数据")
    @GetMapping("/campus-highlights")
    public ResponseEntity<List<CampusHighlightDTO>> getCampusHighlights() {
        List<CampusHighlightDTO> highlights = homeService.getActiveCampusHighlights();
        return ResponseEntity.ok(highlights);
    }

    @Operation(summary = "获取学术研究数据")
    @GetMapping("/academic-research")
    public ResponseEntity<List<AcademicResearchDTO>> getAcademicResearch() {
        List<AcademicResearchDTO> researches = homeService.getActiveAcademicResearches();
        return ResponseEntity.ok(researches);
    }

    @Operation(summary = "获取中间轮播图数据")
    @GetMapping("/middle-slides")
    public ResponseEntity<List<MiddleSlideDTO>> getMiddleSlides() {
        List<MiddleSlideDTO> slides = homeService.getActiveMiddleSlides();
        return ResponseEntity.ok(slides);
    }

    @Operation(summary = "获取英雄区域数据")
    @GetMapping("/hero-section")
    public ResponseEntity<HeroSectionDTO> getHeroSection() {
        HeroSectionDTO heroSection = homeService.getActiveHeroSection();
        if (heroSection == null) {
            // 如果没有数据，返回一个空对象而不是null
            return ResponseEntity.ok(new HeroSectionDTO());
        }
        return ResponseEntity.ok(heroSection);
    }

    @Operation(summary = "获取英雄选项数据")
    @GetMapping("/hero-options")
    public ResponseEntity<List<HeroOptionDTO>> getHeroOptions() {
        List<HeroOptionDTO> options = homeService.getActiveHeroOptions();
        return ResponseEntity.ok(options);
    }

    @Operation(summary = "获取英雄统计数据")
    @GetMapping("/hero-stats")
    public ResponseEntity<List<HeroStatDTO>> getHeroStats() {
        List<HeroStatDTO> stats = homeService.getActiveHeroStats();
        return ResponseEntity.ok(stats);
    }

    @Operation(
        summary = "获取所有首页数据",
        description = "一次性获取所有首页所需的数据，包括轮播图、校园看点、英雄区域、学术研究等"
    )
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "成功获取所有首页数据",
            content = @Content(mediaType = "application/json",
            schema = @Schema(description = "包含所有首页数据的Map")))
    })
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAllHomeData() {
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("slides", homeService.getActiveHomeSlides());
            result.put("campus_highlights", homeService.getActiveCampusHighlights());
            result.put("middle_slides", homeService.getActiveMiddleSlides());

            HeroSectionDTO heroSection = homeService.getActiveHeroSection();
            result.put("hero_section", heroSection != null ? heroSection : new HeroSectionDTO());

            result.put("hero_options", homeService.getActiveHeroOptions());
            result.put("hero_stats", homeService.getActiveHeroStats());
            result.put("academic_research", homeService.getActiveAcademicResearches());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            System.out.println("Error getting all home data: " + e.getMessage());
            // 即使出错也返回一个空的结果集
            Map<String, Object> emptyResult = new HashMap<>();
            emptyResult.put("slides", List.of());
            emptyResult.put("campus_highlights", List.of());
            emptyResult.put("middle_slides", List.of());
            emptyResult.put("hero_section", new HeroSectionDTO());
            emptyResult.put("hero_options", List.of());
            emptyResult.put("hero_stats", List.of());
            emptyResult.put("academic_research", List.of());
            return ResponseEntity.ok(emptyResult);
        }
    }

    @Operation(summary = "测试API是否正常工作")
    @GetMapping("/test")
    public ResponseEntity<String> testApi() {
        return ResponseEntity.ok("HomeController API is working!");
    }
}
